package cn.e4j.modular.oa.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.modular.oa.service.OAAbandonWsService;
import cn.e4j.modular.oa.ws.common.SoapService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import java.util.concurrent.TimeoutException;

@Service
public class OAAbandonWsServiceImpl extends SoapService implements OAAbandonWsService {

    private static Logger logger = LoggerFactory.getLogger(OAAbandonWsService.class);

    @Value("${spring.profiles}")
    String enviroment;

    private static String SOAP_ENDPOINT_URL = null;
    private static String SOAP_ACTION = null;

    @PostConstruct
    private void init() {
        if (enviroment == null) {
            logger.error("获取profiles失败");
            throw new NullPointerException("profiles为null");
        }
        if (enviroment.equals("dev")) {
            // 测试环境
            SOAP_ENDPOINT_URL = "http://192.168.1.234/sys/webservice/flmReviewWebserviceService";
            SOAP_ACTION = "http://192.168.1.234/sys/webservice/flmReviewWebserviceService";
        } else if (enviroment.equals("produce")) {
            // 正式环境
            SOAP_ENDPOINT_URL = "http://ekp.fjlm.com.cn:8093/sys/webservice/flmReviewWebserviceService";
            SOAP_ACTION = "http://ekp.fjlm.com.cn:8093/sys/webservice/flmReviewWebserviceService";
        } else {
            logger.error("未知配置环境（profiles不属于dev/produce）");
            throw new RuntimeException("spring.profiles异常");
        }
    }

    @Override
    public void createBody(SOAPBody soapBody, Dto dto) throws SOAPException {
        SOAPElement soapBodyElem = soapBody.addChildElement("abandonReview", "web");
        SOAPElement arg0 = soapBodyElem.addChildElement("arg0");
        arg0.addTextNode(dto.getString("ID"));
    }

    public String callSoapWs(Dto inDto) throws TimeoutException {
        return super.callSoapWs(SOAP_ENDPOINT_URL,
                SOAP_ACTION, inDto);
    }
}
